# Tests for reference types with short type variables in GDB.
# Copyright 1998-2015 Free Software Foundation, Inc.

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

# written by Elena Zannoni (ezannoni@cygnus.com)

#
# test running programs
#

if { [skip_cplus_tests] } { continue }

standard_testfile .cc

if {[prepare_for_testing $testfile.exp $testfile $srcfile {debug c++}]} {
    return -1
}

#
# set it up at a breakpoint so we can play with the variable values
#
if ![runto_main] then {
    perror "couldn't run to breakpoint"
    continue
}

if ![runto 'marker1'] then {
    perror "couldn't run to marker1"
    continue
}

gdb_test "up" ".*main.*" "up from marker1 1"

proc gdb_start_again {} {
    global srcdir
    global subdir
    global binfile
    global gdb_prompt
    global decimal

    gdb_start
    gdb_reinitialize_dir $srcdir/$subdir
    gdb_load ${binfile}

    #
    # set it up at a breakpoint so we can play with the variable values
    #
    if ![runto_main] then {
	perror "couldn't run to breakpoint"
	continue
    }

    if ![runto 'marker1'] then {
	perror "couldn't run to marker1"
	continue
    }

    gdb_test "up" ".*main.*" "up from marker1 2"
}


gdb_test "print s" ".\[0-9\]* = -1" "print value of s"
gdb_test "ptype s" "type = short"

gdb_test "print *ps" ".\[0-9\]* = -1" "print value of ps"
gdb_test "ptype ps" "type = short \\*"

gdb_test "print as\[0\]" ".\[0-9\]* = 0" "print value of as\[0\]"
gdb_test_multiple "ptype as" "ptype as" {
    -re "type = short \\\[4\\\].*$gdb_prompt $"  { pass "ptype as" }
    -re "type = short int \\\[4\\\].*$gdb_prompt $"  { pass "ptype as" }
}

gdb_test "print as\[1\]" ".\[0-9\]* = 1" "print value of as\[1\]"
gdb_test "print as\[2\]" ".\[0-9\]* = 2" "print value of as\[2\]"
gdb_test "print as\[3\]" ".\[0-9\]* = 3" "print value of as\[3\]"

gdb_test_multiple "print rs" "print value of rs" {
    -re ".\[0-9\]* = \\(short &\\) @$hex: -1.*$gdb_prompt $" {
        pass "print value of rs"
    }
    -re ".\[0-9\]* = \\(short int &\\) @$hex: -1.*$gdb_prompt $" {
        pass "print value of rs"
    }
    eof { fail "print rs ($GDB dumped core) (FIXME)" ; gdb_start_again ; }
}

gdb_test_multiple "ptype rs" "ptype rs" {
    -re "type = short &.*$gdb_prompt $"  { pass "ptype rs" }
    -re "type = short int &.*$gdb_prompt $"  { pass "ptype rs" }
}

gdb_test "print *rps" ".\[0-9\]* = -1" "print value of *rps"

# GDB had a bug about dereferencing a pointer type
# that would lead to wrong results
# if we try to examine memory at pointer value.

gdb_test "x /hd rps" "$hex:\[ \t\]*-1" "examine value at rps"

gdb_test_multiple "ptype rps" "ptype rps" {
    -re "type = short \\*&.*$gdb_prompt $"  { pass "ptype rps" }
    -re "type = short int \\*&.*$gdb_prompt $"  { pass "ptype rps" }
}


gdb_test "print ras\[0\]" ".\[0-9\]* = 0" "print value of ras\[0\]"

gdb_test_multiple "ptype ras" "ptype ras" {
    -re "type = short \\\(&\\\)\\\[4\\\].*$gdb_prompt $"  { pass "ptype ras" }
    -re "type = short int \\\(&\\\)\\\[4\\\].*$gdb_prompt $"  { pass "ptype ras" }
}

gdb_test "print ras\[1\]" ".\[0-9\]* = 1" "print value of ras\[1\]"
gdb_test "print ras\[2\]" ".\[0-9\]* = 2" "print value of ras\[2\]"
gdb_test "print ras\[3\]" ".\[0-9\]* = 3" "print value of ras\[3\]"


if ![runto 'f'] then {
    perror "couldn't run to f"
    continue
}

gdb_test "up" ".main2.*" "up from f"

gdb_test "print C" ".\[0-9\]* = 65 \'A\'" "print value of C"
gdb_test "ptype C" "type = char"

gdb_test "print UC" ".\[0-9\]* = 21 '\.025'" "print value of UC"
gdb_test "ptype UC" "type = unsigned char"

gdb_test "print S" ".\[0-9\]* = -14" "print value of S"
gdb_test "ptype S" "type = short.*"

gdb_test "print US" ".\[0-9\]* = 7" "print value of US"
gdb_test_multiple "ptype US" "ptype US" {
    -re "type = unsigned short.*$gdb_prompt $"  { pass "ptype US" }
    -re "type = short unsigned.*$gdb_prompt $"  { pass "ptype US" }
}

gdb_test "print I" ".\[0-9\]* = 102" "print value of I"
gdb_test "ptype I" "type = int"

gdb_test "print UI" ".\[0-9\]* = 1002" "print value of UI"
gdb_test "ptype UI" "type = unsigned int"

gdb_test "print L" ".\[0-9\]* = -234" "print value of L"
gdb_test "ptype L" "type = long.*"

gdb_test "print UL" ".\[0-9\]* = 234" "print value of UL"
gdb_test_multiple "ptype UL" "ptype UL" {
    -re "type = unsigned long.*$gdb_prompt $"  { pass "ptype UL" }
    -re "type = long unsigned.*$gdb_prompt $"  { pass "ptype UL" }
}

gdb_test "print F" ".\[0-9\]* = 1.2\[0-9\]*e\\+0?10.*" \
    "print value of F"

gdb_test "ptype F" "type = float.*"

gdb_test "print D" ".\[0-9\]* = -1.375e-123.*" \
    "print value of D"

gdb_test "ptype D" "type = double.*"


#
# test reference types
#

gdb_test "ptype rC" "type = char &"

gdb_test "ptype rUC" "type = unsigned char &"

gdb_test_multiple "ptype rS" "ptype rS" {
    -re "type = short &.*$gdb_prompt $"  { pass "ptype rS" }
    -re "type = short int &.*$gdb_prompt $"  { pass "ptype rS" }
}

gdb_test_multiple "ptype rUS" "ptype rUS" {
    -re "type = unsigned short &.*$gdb_prompt $"  { pass "ptype rUS" }
    -re "type = short unsigned int &.*$gdb_prompt $"  { pass "ptype rUS" }
}

gdb_test "ptype rI" "type = int &"

gdb_test "ptype rUI" "type = unsigned int &"

gdb_test_multiple "ptype rL" "ptype rL" {
    -re "type = long &.*$gdb_prompt $"  { pass "ptype rL" }
    -re "type = long int &.*$gdb_prompt $"  { pass "ptype rL" }
}

gdb_test_multiple "ptype rUL" "ptype rUL" {
    -re "type = unsigned long &.*$gdb_prompt $"  { pass "ptype rUL" }
    -re "type = long unsigned int &.*$gdb_prompt $"  { pass "ptype rUL" }
}

gdb_test "ptype rF" "type = float &"

gdb_test "ptype rD" "type = double &"

gdb_test "print rC" ".\[0-9\]* = \\(char &\\) @$hex: 65 \'A\'" \
    "print value of rC"

gdb_test "print rUC" \
    ".\[0-9\]* = \\(unsigned char &\\) @$hex: 21 \'.025\'" \
    "print value of rUC"

gdb_test_multiple "print rS" "print value of rS" {
    -re ".\[0-9\]* = \\(short &\\) @$hex: -14.*$gdb_prompt $" {
        pass "print value of rS"
    }
    -re ".\[0-9\]* = \\(short int &\\) @$hex: -14.*$gdb_prompt $" {
        pass "print value of rS"
    }
}

gdb_test_multiple "print rUS" "print value of rUS" {
    -re ".\[0-9\]* = \\(unsigned short &\\) @$hex: 7.*$gdb_prompt $" {
        pass "print value of rUS"
    }
    -re ".\[0-9\]* = \\(short unsigned int &\\) @$hex: 7.*$gdb_prompt $" {
        pass "print value of rUS"
    }
}

gdb_test "print rI" ".\[0-9\]* = \\(int &\\) @$hex: 102" \
	"print value of rI"

gdb_test "print rUI" \
    ".\[0-9\]* = \\(unsigned int &\\) @$hex: 1002" \
        "print value of UI"

gdb_test_multiple "print rL" "print value of rL" {
    -re ".\[0-9\]* = \\(long &\\) @$hex: -234.*$gdb_prompt $" {
        pass "print value of rL"
    }
    -re ".\[0-9\]* = \\(long int &\\) @$hex: -234.*$gdb_prompt $" {
        pass "print value of rL"
    }
}

gdb_test_multiple "print rUL" "print value of rUL" {
    -re ".\[0-9\]* = \\(unsigned long &\\) @$hex: 234.*$gdb_prompt $" {
        pass "print value of rUL"
    }
    -re ".\[0-9\]* = \\(long unsigned int &\\) @$hex: 234.*$gdb_prompt $" {
        pass "print value of rUL"
    }
}

gdb_test "print rF" \
    ".\[0-9\]* = \\(float &\\) @$hex: 1.2\[0-9\]*e\\+0?10.*" \
    "print value of rF"

gdb_test "print rD" \
    ".\[0-9\]* = \\(double &\\) @$hex: -1.375e-123.*" \
    "print value of rD"

